﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsCalculate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string resultString = "";
            //公式
            var curGongshi = gongshi.Text == null ? "" : gongshi.Text.Replace(" ", "").Trim();
            resultString += $"公式结果：\n{curGongshi}\n{new DataTable().Compute(curGongshi, null)}\n ";

            this.result.Text += resultString;
            //方程组 未知数
            var curFangchengzu = fangchengzu.Text.Split('\n').ToList()
                .Where(data => data.Replace(" ", "") != null && data.Replace(" ", "") != "")
                .ToList();
            var curWeizhishu = weizhishu.Text.Split('\n').ToList()
                .Where(data => data.Replace(" ", "") != null && data.Replace(" ", "") != "")
                .ToList();
            if (curWeizhishu.Count == 1)
            {
                for (double i = -100; i < 100; i = i + 0.01)
                {
                    var curFc = curFangchengzu[0];
                    if (curFc.Contains(curWeizhishu[0]))
                    {
                        var newFc = curFc.Replace(curWeizhishu[0], $"({i.ToString()})");
                        var twoside = newFc.Split('=').ToList();
                        var rr1 = "";
                        var rr2 = "";
                        if (twoside.Count == 2)
                        {
                            rr1 = new DataTable().Compute(twoside[0], null).ToString();
                            rr2 = new DataTable().Compute(twoside[1], null).ToString();
                            if (Math.Abs(Convert.ToDouble(rr1) - Convert.ToDouble(rr2)) <= 0.00001)
                            {
                                this.result.Text += $"\n未知数结果：\n{curWeizhishu[0]}={i.ToString("0.00")}";
                                break;
                            }
                        }
                    }
                }
            }

            if (curWeizhishu.Count == 2)
            {
                var curFc1 = curFangchengzu[0];
                var curFc2 = curFangchengzu[1];
                for (double i = -100; i < 100; i = i + 0.1)
                {
                    var newfc1 = "";
                    if (curFc1.Contains(curWeizhishu[0]))
                    {
                        newfc1 = curFc1.Replace(curWeizhishu[0], $"({i.ToString()})");
                    }
                    for (double j = -100; j < 100; j = j + 0.1)
                    {
                        var newfc11 = "";
                        if (newfc1.Contains(curWeizhishu[1]))
                        {
                            newfc11 = newfc1.Replace(curWeizhishu[1], $"({j.ToString()})");
                        }
                        var twoside = newfc11.Split('=').ToList();
                        var rr1 = "";
                        var rr2 = "";
                        if (twoside.Count == 2)
                        {
                            rr1 = new DataTable().Compute(twoside[0], null).ToString();
                            rr2 = new DataTable().Compute(twoside[1], null).ToString();
                            if (Math.Abs(Convert.ToDouble(rr1) - Convert.ToDouble(rr2)) <= 0.00001)
                            {
                                //满足第一个公式，再验证是否满足第二个
                                var newcurFc2= curFc2.Replace(curWeizhishu[0], $"({i.ToString()})")
                                    .Replace(curWeizhishu[1], $"({j.ToString()})");

                                var twoside2 = newcurFc2.Split('=').ToList();
                                var rr21 = "";
                                var rr22 = "";

                                if (twoside2.Count == 2)
                                {
                                    rr21 = new DataTable().Compute(twoside2[0], null).ToString();
                                    rr22 = new DataTable().Compute(twoside2[1], null).ToString();
                                    if (Math.Abs(Convert.ToDouble(rr21) - Convert.ToDouble(rr22)) <= 0.00001)
                                    {
                                        this.result.Text += $"\n未知数结果：\n{curWeizhishu[0]}={i.ToString("0.00")}\n{curWeizhishu[1]}={j.ToString("0.00")}";
                                        break;

                                    }
                                }
                            }
                        }
                    }
                }
            }


            //for (double i = -100; i < 100; i = i + 0.01)
            //{
            //    foreach (var fc in curFangchengzu)
            //    {
            //        var curFc = fc;
            //        if (curWeizhishu.Count(data => curFc.Contains(data)) > 0)
            //        {
            //            foreach (var wz in curWeizhishu)
            //            {
            //                if (curFc.Contains(wz))
            //                {
            //                    var newFc = curFc.Replace(wz, i.ToString());
            //                }
            //            }
            //        }
            //    }
            //}
        }

        private void 字符串处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Task.Run(() =>
            //{
                new TextHandle.textHandle().Show();
            //});
        }
    }
}
